Package com.google.gwt.sample.gwtguestbook.client

Source Code of com.google.gwt.sample.gwtguestbook.client.GuestSignaturePanel

/*
* Copyright 2009 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/
package com.google.gwt.sample.gwtguestbook.client;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.KeyCodes;
import com.google.gwt.event.dom.client.KeyPressEvent;
import com.google.gwt.event.dom.client.KeyPressHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.TextBox;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
* Guest signature widget class, containing the components needed to sign the
* guestbook.
*/
public class GuestSignaturePanel extends Composite implements FiresEntryUpdates {
  private HorizontalPanel guestSignaturePanel;
  private TextBox guestNameBox;
  private Label guestNameLabel;
  private TextBox guestMessageBox;
  private Label guestMessageLabel;
  private Button signButton;
  private boolean guestSignatureError = false;
  private List<GuestbookEntryDTO> localEntries = new ArrayList<GuestbookEntryDTO>();
  private GuestbookEntryDTO entry;
  private static GuestServiceAsync guestService = (GuestServiceAsync) GWT
      .create(GuestService.class);

  /* Register for entry update handlers listening for guest signatures */
  private List<EntryUpdateHandler> entryUpdateHandlers;

  /**
   * Creates a guest signature panel, with components fully instantiated and
   * attached.
   *
   * The {@link com.google.gwt.user.client.ui.Composite#initWidget(Widget)}
   * method must be called exactly once in the constructor to declare the
   * widget that this composite class is wrapping.
   */
  public GuestSignaturePanel() {
    entryUpdateHandlers = new ArrayList<EntryUpdateHandler>();
    guestSignaturePanel = new HorizontalPanel();

    // Create guest signature panel widgets
    guestNameBox = new TextBox();
    guestMessageBox = new TextBox();
    guestNameLabel = new Label("Name:");
    guestMessageLabel = new Label("Message:");
    signButton = new Button("Sign!");

    // Set up the widget guest signature panel widget styles (additional to
    // standard styles)
    guestNameLabel.addStyleName("gb-Label");
    guestMessageLabel.addStyleName("gb-Label");
    guestNameBox.addStyleName("gb-NameBox");
    guestMessageBox.addStyleName("gb-MessageBox");
    // Listen for keyboard events in the input box.
    guestMessageBox.addKeyPressHandler(new KeyPressHandler() {
      public void onKeyPress(KeyPressEvent event) {
        if (event.getCharCode() == KeyCodes.KEY_ENTER) {
          sign();
        }
      }
    });

    // Attach components together
    guestSignaturePanel.add(guestNameLabel);
    guestSignaturePanel.add(guestNameBox);
    guestSignaturePanel.add(guestMessageLabel);
    guestSignaturePanel.add(guestMessageBox);
    guestSignaturePanel.add(signButton);

    signButton.addClickHandler(new ClickHandler() {
      public void onClick(ClickEvent event) {
        sign();
      }
    });
    initWidget(guestSignaturePanel);
  }

  /**
   * Listens for the guest entry update event, and persists the guest entry
   */
  @Override
  public void sign() {
    if ("".equals(guestNameBox.getText())
        || "".equals(guestMessageBox.equals(""))) {
      guestSignatureError = true;
      fireUpdateError("Please make sure you've filled in all fields");
    } else {
      // Make the entry
      entry = new GuestbookEntryDTO(guestNameBox.getText(),
          guestMessageBox.getText(), new Date()); // dummy date
      guestService.addGuestEntry(entry,
          new AsyncCallback<List<GuestbookEntryDTO>>() {
            @Override
            public void onFailure(Throwable caught) {
              fireUpdateError("Failed to add new entry to guest book");
            }

            @Override
            public void onSuccess(List<GuestbookEntryDTO> result) {
              if(result!=null){
              localEntries = result;
              GWT.log(result.get(0).getMessage() + " "+ entry.getMessage());
                if(!result.get(0).getMessage().equals(entry.getMessage())){
                  localEntries.add(0,entry);
                }
              }else{
                localEntries.add(0,entry);
              }
             
              // Fire an update
              fireUpdate(localEntries);
              // if(localEntries.contains(entry)){
              // ;
              // }
              Window.alert("Data by " + guestNameBox.getValue()
                  + " has been added");
              // guestNameBox.setText("");
              guestMessageBox.setText("");
              guestNameBox.setFocus(true);
            }
          });
    }
  }

  /**
   * Fires an update error and passes the error message to all registered
   * update handlers
   *
   * @param errorMessage
   *            the error message to pass on
   */
  protected void fireUpdateError(String errorMessage) {
    for (EntryUpdateHandler handler : entryUpdateHandlers) {
      handler.updateError(errorMessage);
    }
  }

  /**
   * Fires an update and passes the entries to all registered update handlers
   *
   * @param entries
   *            the latest guest entries to be displayed
   */
  protected void fireUpdate(List<GuestbookEntryDTO> entries) {
    for (EntryUpdateHandler handler : entryUpdateHandlers) {
      // Signal that any previous sign in errors are now cleared to all
      // registered handlers
      if (guestSignatureError) {
        handler.clearError();
        guestSignatureError = false;
      }

      handler.updateEntries(entries);
    }
  }

  @Override
  public void addEntryUpdateHandler(EntryUpdateHandler handler) {
    entryUpdateHandlers.add(handler);
  }

  @Override
  public void removeEntryUpdateHandler(EntryUpdateHandler handler) {
    entryUpdateHandlers.remove(handler);
  }
}
TOP

Related Classes of com.google.gwt.sample.gwtguestbook.client.GuestSignaturePanel

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.